<?php

namespace seecms;

use seecms\lib\module\ModuleConfig;

/**
 * see处理模块类
 * 单列模式
 */
class SeeModule
{
    /**
     * @var SeeModule
     */
    protected static $instance = null;

    /**
     * 语言包
     * @var array
     */
    protected $lang = [];

    /**
     * 视图
     * @var array
     */
    protected $view = [];

    /**
     * 控制器
     * @var array
     */
    protected $controller = [];


    protected function __construct()
    {

    }

    protected function __clone()
    {

    }

    /**
     * @return SeeModule
     */
    public static function instance(): SeeModule
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
            if ($modules = self::$instance->get()) {
                foreach ($modules as $module) {
                    if (class_exists($module)) {
                        $cla = new $module();
                        if ($cla instanceof ModuleConfig) {
                            self::$instance->controller = array_merge(self::$instance->controller, $cla->controller());
                            self::$instance->lang[] = $cla->lang();
                            self::$instance->view[] = $cla->view();
                        }
                    }
                }
            }
        }

        return self::$instance;
    }

    public function getLang(): array
    {
        return self::$instance->lang;
    }

    public function getView(): array
    {
        return self::$instance->view;
    }

    public function getController(): array
    {
        return self::$instance->controller;
    }

    public function get(): array
    {
        return See::config()->value('module') ?: [];
    }
}